﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.sysDAL;
using dhDataAcess.Model;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.baseBLL;

namespace dhSystemBLL.basicData
{
   public class accessGroupBLL
    {
       private accessGroupTableAdapter _accessGroupAdapter = null;
       private accessGroupTableAdapter  accessGroupAdapter
        {
            get
            {
                if (_accessGroupAdapter == null) _accessGroupAdapter = new accessGroupTableAdapter ();
                return _accessGroupAdapter;
            }
        }
        /// <summary>
       /// 查询权限组表格
        /// </summary>
        /// <returns></returns>

       public sysDataSet.accessGroupDataTable  getaccessGroupAll()
        {
            try
            {
                return accessGroupAdapter.GetDataAccessGroupAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

       /// <summary>
       /// 根据权限组编号查询表
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>

       public sysDataSet.accessGroupDataTable  getaccessGroupAll(string groupNum)
        {
            try
            {

                return accessGroupAdapter.GetDataaccessGroupByNum (groupNum );

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }




        /// <summary>
       /// 插入信息
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

       public int accessGroupInsert(accessGroupModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                accessGroupDAL accessGroup = new accessGroupDAL(tn, conn);
                accessGroup.accessGroupInsert(row);
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个权限组编号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

       public int accessGroupUpdate(accessGroupModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                accessGroupDAL accessGroup = new accessGroupDAL(tn, conn);
                accessGroup.accessGroupUpdate(row);
                tn.Commit();
                return i;
                
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 删除
        /// </summary>
        /// <param name="itemNum"></param>
        /// <returns></returns>

       public int accessGroupDel(string groupNum)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                accessGroupDAL accessGroup = new accessGroupDAL(tn, conn);
                accessGroup.accessGroupDel(groupNum);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 根据权限编号返回权限名称
        /// </summary>
        /// <param name="itemNum"></param>
        /// <returns></returns>

       public int accessGroupRetName(string groupNum)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                accessGroupDAL accessGroup = new accessGroupDAL(tn, conn);
                accessGroup.accessGroupretname(groupNum);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
